import { UserInfo } from "../common/UserInfo";
import { Building } from "../entity/Buildings";
import { EventManager } from "../event/EventManager";
import { Debug } from "./Debug";

export class BuildingLocalApi {
  
    /**建造或升级完成时调用 */
    public static buildComplete(building: Building) {
        
        UserInfo.instance.removeEvent(building.getBuildingEvent())
        let bm = UserInfo.instance.getBManage(building.uid)
        bm.grade++
       
        EventManager.instance.emit(EventManager.EventType.GetVillageDetail, UserInfo.instance.villageData)
    }
 
    /**拆除完成时调用 */
    public static removeComplete(building: Building) {
        UserInfo.instance.removeEvent(building.getBuildingEvent())
        let bm = UserInfo.instance.getBManage(building.uid)
        bm.gid = 0
        bm.grade = 0
        bm.instantHint = ''
      
        EventManager.instance.emit(EventManager.EventType.GetVillageDetail, UserInfo.instance.villageData)
    }
    /**取消 */
    public static cancel(building: Building) {
       
        Debug.logRes()
        
        // 取消成功后还要返还资源
        if(building.isBuilding){
            let nextBuilding = Building.createByIdAndLevel(building.id, building.level+ 1)
            let villageData = UserInfo.instance.villageData
            villageData.res.food_reserve += nextBuilding.food
            villageData.res.iron_reserve += nextBuilding.iron
            villageData.res.mud_reserve += nextBuilding.mud
            villageData.res.wood_reserve += nextBuilding.wood

            if(villageData.res.food_reserve > villageData.res.food_max_reserve){
                villageData.res.food_reserve = villageData.res.food_max_reserve
            }
            if(villageData.res.iron_reserve > villageData.res.comm_max_reserve){
                villageData.res.iron_reserve = villageData.res.comm_max_reserve
            }
            if(villageData.res.mud_reserve > villageData.res.comm_max_reserve){
                villageData.res.mud_reserve = villageData.res.comm_max_reserve
            }
            if(villageData.res.wood_reserve > villageData.res.comm_max_reserve){
                villageData.res.wood_reserve = villageData.res.comm_max_reserve
            }
        }
       
       


        UserInfo.instance.removeEvent(building.getBuildingEvent())
       
        Debug.logRes()
        EventManager.instance.emit(EventManager.EventType.GetVillageDetail, UserInfo.instance.villageData)
    }
    /**加速 */
    public static speed(building: Building) {
        UserInfo.instance.removeEvent(building.getBuildingEvent())
       

        UserInfo.instance.refreshVillageDetail()
       
    }
}


